package com.news.web.servlet.news;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.news.entity.News;
import com.news.entity.Review;
import com.news.entity.User;
import com.news.service.NewsService;
import com.news.service.ReviewService;
import com.news.service.UserService;
import com.news.service.impl.NewsServiceImpl;
import com.news.service.impl.ReviewServiceImpl;
import com.news.service.impl.UserServiceImpl;

/**
 * Servlet implementation class NewsReviewServlet
 */
@WebServlet("/NewsReviewServlet")
public class NewsReviewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 通过从session里面取出用户名和密码查询id
		//获取session，如果没有，跳转到登录页面
		HttpSession session = request.getSession();
		if(session.getAttribute("userName")==null||session.getAttribute("userPass")==null) {
			// 将数据存入
			request.setAttribute("newsUserReview", null);
			// 跳转回原页面
			request.getRequestDispatcher("logIn.jsp").forward(request, response);
		}
		
		// 获取传值，需要对应新闻id，评论内容,id由于是转发，所以不可能为空值
		String newsIdStr = request.getParameter("newsId");
		String reviewDetails = request.getParameter("reviewDetails");

		int newsId = 0;
		try {
			newsId = Integer.parseInt(newsIdStr);
			System.out.println("The integer value is: " + newsId);
		} catch (NumberFormatException e) {
			System.err.println("id错误，转发的id为：" + newsIdStr);
		}
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 创建UserService实例
		UserService userService = new UserServiceImpl();
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		
		String userName = (String) session.getAttribute("userName");
		String userPass = (String) session.getAttribute("userPass");
		// 获取对应的用户
		User user = userService.selectUser(userName, userPass);

		// 获取userId
		int userId = user.getUserId();
		// 获取评论时间
		Timestamp reviewDate = new Timestamp(System.currentTimeMillis());

		// 创建评论实例
		Review review = new Review();
		review.setUserId(userId);
		review.setReviewDate(reviewDate);
		review.setNewsId(newsId);
		review.setReviewDetails(reviewDetails);

		// 添加评论
		Boolean newsReview = reviewService.insertReview(review);

		// 创建map
		Map<String, Object> singleMap = new HashMap<String, Object>();
		// 返回原新闻页面
		// 查询对应新闻
		News news = newsService.queryNews(newsId);
		// 添加新闻热度
		news.setNewsHeat(news.getNewsHeat() + 1);
		// 修改新闻
		newsService.updateNews(news);
		//更新新闻评论数量
		newsService.selectNewsReviewAndUpdate(newsId);
		// 数据写入map
		singleMap.put("news", news);
		// 查询新闻对应的评论
		List<Review> reviewList = reviewService.queryReviewInNews(newsId);
		// 数据写入map
		singleMap.put("reviewList", reviewList);
		// 查询相关新闻,传入类型和需要的条数
		List<News> relatedNewsList = newsService.queryNewsTypeNum(news.getNewsType(), 4);
		// 数据写入map
		singleMap.put("relatedNewsList", relatedNewsList);
		// 查询右侧栏热度最高的四条新闻
		List<News> queryRightNewsHeatNumList = newsService.queryNewsHeatNum(4);
		// 装入数据
		singleMap.put("queryRightNewsHeatNumList", queryRightNewsHeatNumList);
		// 查询右侧栏评论最高的四条新闻
		List<News> queryNewsReviewNum = newsService.queryNewsReviewNum(4);
		// 装入数据
		singleMap.put("queryNewsReviewNum", queryNewsReviewNum);
		// 将数据存入
		request.setAttribute("singleMap", singleMap);
		// 跳转回原页面
		request.getRequestDispatcher("single.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
